<?php

/**
 * BannerListController
 * 
 * @author
 * @version 
 */

require_once 'Zend/Controller/Action.php';

class Cms_BannerListController extends Zend_Controller_Action {
	/**
	 * The default action - show the home page
	 */
	public function indexAction() {
		CmsAuth::check();
		
		if (NULL === $id = $this->getRequest()->getParam('id')) {
			throw new Zend_Controller_Action_Exception('Required param "Id"', 404);
		}
		
		$bannerList = MF_Main::getInstance()->BannerList->getById($id);
		
		$this->view->banner_list_id = $bannerList->_content_id;
		$this->view->extra_data = $bannerList->config;
	}

	
	
	public function dataAction() {
		CmsAuth::check();
		
		if (NULL === $id = $this->getRequest()->getParam('id')) {
			throw new Zend_Controller_Action_Exception('Required param "Id"', 404);
		}
		
		$banerList = MF_Main::getInstance()->BannerList->getById($id);
		
		
		if (NULL !== $act = $this->getRequest()->getParam('act')) {
			switch ($act) {
				case 'add':
					$banerList->add();
					break;
				
				case 'del':
					if (NULL === $banner_id = $this->getRequest()->getParam('banner_id')) {
						throw new Zend_Controller_Action_Exception('Required param "banner_id" while del action precessed', 404);
					}
					$banerList->getById($banner_id)->delete();
					break;
					
				default:
					;
				break;
			}
			
		}
		
		
		$data = array();
		for ($i=0;$i<$banerList->length();$i++) {
			$banner_data = array(
					array('banner_name',
						array('id' => $banerList->item($i)->_content_id, 
							'value' => $banerList->item($i)->name)),
						
					$this->_helper->WidgetAbstract($banerList->item($i)->image),
					
					array('banner_link',
						array('id' => $banerList->item($i)->_content_id, 
							'value' => $banerList->item($i)->link))
				);
				
			for ($t=0;$t<count($banerList->config);$t++) {
				$key_name = $banerList->config[$t][0];
				array_push($banner_data,
					$this->_helper->WidgetAbstract($banerList->item($i)->$key_name));
			}
			
			array_push($banner_data,
				/*array('image_up',array('id' => $galleryItem->image($i)->_content_id)),
				array('image_down',array('id' => $galleryItem->image($i)->_content_id)),*/
				array('banner_delete',array('id' => $banerList->item($i)->_content_id))
			);
			
			array_push($data,$banner_data);
		}
		
		$this->view->data = $data;
		$this->renderScript('json.phtml');
	}
	
	public function changeAction() {
		CmsAuth::check();
		
		if (NULL === $id = $this->getRequest()->getParam('id')) {
			throw new Zend_Controller_Action_Exception('Required param "Id"', 404);
		}
		if (NULL === $banner_list_id = $this->getRequest()->getParam('banner_list_id')) {
			throw new Zend_Controller_Action_Exception('Required param "gallery_id"', 404);
		}
		
		$galleryImage = MF_Main::getInstance()->BannerList->getById($banner_list_id)->getById($id);
		
		if (NULL !== $title = $this->getRequest()->getParam('name')) {
			$title = $title; 
			$galleryImage->name = $title;
		}
	
		if (NULL !== $link = $this->getRequest()->getParam('link')) {
			$title = $title; 
			$galleryImage->link = $link;
		}
	
		
		$this->view->result = '1';
		$this->_helper->layout->disableLayout(); 
	}
	
	public function filledAction() {
		CmsAuth::check();
		
		if (NULL === $id = $this->getRequest()->getParam('id')) {
			throw new Zend_Controller_Action_Exception('Required param "Id"', 404);
		}
		
		$this->view->result = MF_Main::getInstance()->BannerList->getById($id)->length()==0 ? '0' : '1'; 
		$this->_helper->layout->disableLayout();
	}
}